//
//  WLVersionUpdateView.m
//  WanLian
//
//  Created by muzhuang on 2018/10/25.
//  Copyright © 2018年 wanlian. All rights reserved.
//

#import "WLVersionUpdateView.h"

@interface WLVersionUpdateView ()

@property (nonatomic,copy) NSDictionary *dictVer;
@property (nonatomic,strong) UIView *bgView;

@end

@implementation WLVersionUpdateView


- (instancetype)initWithInfo:(NSDictionary *)dictInfo
{
    self = [super init];
    if (self) {
        _dictVer = dictInfo;
        self.bounds = self.frame = [[UIScreen mainScreen]bounds];
        self.backgroundColor = wlRGBACOLOR(0, 0, 0, 0.5);
        self.alpha = 0;
        [self setupUI];
    }
    return self;
}

- (void)setupUI
{
    _bgView = [[UIView alloc] initWithFrame:CGRectMake((WIDTH_SCREEN_WL-300)/2.0, (HEIGHT_SCREEN_WL-320)/2.0, 300, 320)];
    [self addSubview:_bgView];
    
    UIImageView *ivBack = [[UIImageView alloc] initWithFrame:_bgView.bounds];
    ivBack.image = [UIImage imageNamed:@"version_update"];
    [_bgView addSubview:ivBack];
    
    UIImageView *ivTop = [[UIImageView alloc] initWithFrame:CGRectMake(150-29.5, -49, 59, 123)];
    ivTop.image = [UIImage imageNamed:@"ver_update_tip"];
    [_bgView addSubview:ivTop];
    
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(25, 120, 250, 32)];
    titleLabel.textColor = wlBlackColor ;
    titleLabel.font = [WLCommonTool fontTypePingFangSCMedium:20];
    titleLabel.text = @"发现新版本";//_theNewVersion.title.length<=0?@"发现新版本":_theNewVersion.title;
    titleLabel.textAlignment = NSTextAlignmentCenter;
    [self.bgView addSubview:titleLabel];
    
    NSString *strContent = _dictVer[@"updateLog"];
    strContent = [strContent stringByReplacingOccurrencesOfString:@";"withString:@"\n"];
    UITextView *textContent = [[UITextView alloc] initWithFrame:CGRectMake(25, 165, 250, 320-165-44-16)];
    textContent.backgroundColor = [UIColor clearColor];
    textContent.font = [WLCommonTool fontTypePingFangSCRegular:14];
    textContent.textColor = wlDarkGrayColor;
    textContent.editable = NO;
    textContent.text = strContent;
    [self.bgView addSubview:textContent];
    
    UIView *lineHoriz = [[UIView alloc] initWithFrame:CGRectMake(0, 320-45, 300, 1)];
    lineHoriz.backgroundColor = wlUIColorRGB(0xF2F2F2);
    [self.bgView addSubview:lineHoriz];
    
//    if ([_dictVer[@"forceUpdata"] integerValue] == 0)//不是强制升级
//    {
//        UIButton *closeBtn = [[UIButton alloc] init];
//        [closeBtn setImage:[UIImage imageNamed:@"yx_ad_close"] forState:UIControlStateNormal];
//        closeBtn.imageView.contentMode = UIViewContentModeScaleAspectFill;
//        [closeBtn addTarget:self action:@selector(closeAction) forControlEvents:UIControlEventTouchUpInside];
//        [self addSubview:closeBtn];
//    }
    BOOL bForceUpdata = [_dictVer[@"object"][@"bForceUpdata"] boolValue];
    if (bForceUpdata == NO) {
        UIButton *sureBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 320-44, 300, 44)];
        sureBtn.titleLabel.font = [WLCommonTool fontTypePingFangSCMedium:17];
        [sureBtn setTitleColor:wlblueColor forState:UIControlStateNormal];
        [sureBtn setTitle:@"立即升级" forState:UIControlStateNormal];
        [sureBtn addTarget:self action:@selector(sureAction) forControlEvents:UIControlEventTouchUpInside];
        [_bgView addSubview:sureBtn];
    } else {
        
        UIButton *cancelBtn = [[UIButton alloc] initWithFrame:CGRectMake(5, 320-44, 140, 44)];
        cancelBtn.titleLabel.font = [WLCommonTool fontTypePingFangSCMedium:17];
        [cancelBtn setTitleColor:wlUnderlineColor forState:UIControlStateNormal];
        [cancelBtn setTitle:@"以后再说" forState:UIControlStateNormal];
        [cancelBtn addTarget:self action:@selector(closeAction) forControlEvents:UIControlEventTouchUpInside];
        [_bgView addSubview:cancelBtn];
        
        UIView *lineVertical = [[UIView alloc] initWithFrame:CGRectMake(150, 320-37, 1, 30)];
        lineVertical.backgroundColor = wlUIColorRGB(0xF2F2F2);
        [self.bgView addSubview:lineVertical];
        
        UIButton *sureBtn = [[UIButton alloc] initWithFrame:CGRectMake(155, 320-44, 140, 44)];
        sureBtn.titleLabel.font = [WLCommonTool fontTypePingFangSCMedium:17];
        [sureBtn setTitleColor:wlblueColor forState:UIControlStateNormal];
        [sureBtn setTitle:@"立即升级" forState:UIControlStateNormal];
        [sureBtn addTarget:self action:@selector(sureAction) forControlEvents:UIControlEventTouchUpInside];
        [_bgView addSubview:sureBtn];
    }
    
}

- (void)show
{
    UIWindow *window =  [[[UIApplication sharedApplication] delegate] window];
    BOOL hasMask = NO;
    for(UIView *objc in window.subviews){
        if([objc isKindOfClass:[self class]]){
            hasMask = YES;
        }
    }
    if(hasMask){
        return;
    }
    if (self.superview==nil) {
        [window addSubview:self];
    }
    self.alpha = 0;
    [UIView animateWithDuration:0.25 animations:^{
        self.alpha = 1;
    }];
}

- (void)hide
{
    if (self.superview) {
        [UIView animateWithDuration:0.25 animations:^{
            self.alpha = 0;
        } completion:^(BOOL finished) {
            [self removeFromSuperview];
        }];
    }
}

- (void)sureAction
{
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:_dictVer[@"downUrl"]]];
}

- (void)closeAction
{
    [self hide];
//    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
//
//    });
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
